Skip to content

fix: handle Field(default_factory=...) in @tool decorator without warning#2200

Open
luyao618 wants to merge 1 commit into
strands-agents:mainfrom
luyao618:fix/tool-decorator-fieldinfo-default
Open

fix: handle Field(default_factory=...) in @tool decorator without warning#2200
luyao618 wants to merge 1 commit into
strands-agents:mainfrom
luyao618:fix/tool-decorator-fieldinfo-default

Conversation

@luyao618
Copy link
Copy Markdown

Problem

When a @tool-decorated function uses Field(default_factory=list) as a parameter default:

@tool
def my_tool(items: list = Field(default_factory=list)):
    ...

The decorator passes the FieldInfo object as default=<FieldInfo> into another Field() call, producing a non-serialisable default that triggers PydanticJsonSchemaWarning.

Reported in #1914.

Solution

In _extract_annotated_metadata(), detect when param_default is already a FieldInfo instance and extract its default/default_factory/description attributes to build a clean Field() instead of wrapping it again.

Testing

Added two tests:

  • test_tool_field_default_factory_no_warning — verifies no PydanticJsonSchemaWarning is emitted and the schema is correct
  • test_tool_field_default_value — verifies Field(default=...) as a parameter default works correctly

Closes #1914

…ning

When a @tool-decorated function uses Field(default_factory=list) as a
parameter default, the decorator wraps the FieldInfo in another Field()
call, producing a non-serialisable default that triggers
PydanticJsonSchemaWarning.

Detect when param_default is already a FieldInfo instance and extract
its default/default_factory/description instead of wrapping it again.

Closes strands-agents#1914
@yonib05 yonib05 added the area-tool Tool behavior/api label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-tool Tool behavior/api size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] @tool emits PydanticJsonSchemaWarning for Field(default_factory=list)

2 participants